package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Question;
import cn.wolfcode.luowowo.article.domain.QuestionTag;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.service.IDestinatonService;
import cn.wolfcode.luowowo.article.service.IQuestionService;
import cn.wolfcode.luowowo.article.service.IQuestionTagService;
import cn.wolfcode.luowowo.article.service.IRegionService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.UMEditorUploader;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2019/7/18.
 */
@Controller
@RequestMapping("question")
public class QuestionController {
    @Reference
    private IRegionService regionService;
    @Reference
    private IQuestionTagService questionTagService;
    @Reference
    private IDestinatonService destinatonService;
    @Reference
    private IQuestionService questionService;

    @RequestMapping("index")
    public String index(){
        return "question/index";
    }

    //需要登录才能提问
    @RequireLogin
    @RequestMapping("input")
    public String publish(Model model){
       /* List<Region> list = regionService.queryHotRegions();
        model.addAttribute("hotRegions",list);*/
        List<Destination> list = destinatonService.list();
        model.addAttribute("dests",list);
        List<QuestionTag> questionTags = questionTagService.listAll();
        model.addAttribute("questionTags",questionTags);
        return "question/input";
    }
    //需要登录才能发布问题
    @RequestMapping("save")
    @RequireLogin
    public String save(@UserParam UserInfo userInfo, Question question){
        question.setUserInfo(userInfo);
        questionService.saveOrUpdate(question);
        return "question/index";
    }
    //需要登录才能发布问题
    @RequestMapping("detail")
    @RequireLogin
    public String detail(){
       return "question/detail";
    }











    @Value("${file.path}")
    private String filePath;
    @RequestMapping("/coverImageUpload")
    @ResponseBody
    public Object coverImageUpload(MultipartFile pic){
        String upload = UploadUtil.upload(pic, filePath);
        return upload;
    }

    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile,HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, filePath);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }


}
